using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class GameManger : MonoBehaviour
{
    // Start is called before the first frame update
    public static GameManger instance;
    public Train train;

    public TrainControlAdaptor trainControlAdaptor;

    private int railInterval_gid = 0;

    public StrengthSlider slider;

    public GameObject endPanel;
    public Text endText;

    public LifeUI life;

    private void Awake()
    {
        instance = this;
        endPanel.SetActive(false);    
    }

    private void Update() 
    {
        if(life.curIndex == 5)
        {
            GameFail();
        }
    }

    public float getTrainMinSpeed()
    {
        return train.defaultSpeed;
    }

    public void RegisteredRailInterval(RailwayInterval railwayInterval)
    {
        if (railwayInterval.gid != 0 && railwayInterval.gid > railInterval_gid)
        {
            Debug.LogWarning("invail railwayInterval");
        }
        railwayInterval.gid = this.railInterval_gid++;
    }

    public void SetRailWayRange(float left, float right, float time = -1)
    {
        slider.SetArea(left, right, time);
    }

    public void CloseRailWayRange()
    {
        slider.CloseArea();
    }

    public void SetRealAcc(float acc = 0f)
    {
        trainControlAdaptor.SetAcceleration(acc);
    }

    public void StartGame()
    {
        SceneManager.LoadSceneAsync(1);
    }

    public void ExitGame()
    {
        Application.Quit();
    }

    public void GameFail()
    {
        endPanel.SetActive(true);
        endText.text = "Game Over ! ";
    }

    public void GameWin()
    {
        endPanel.SetActive(true);
        endText.text = "You win ! ";
    }
}
